package com.aosgame;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;

import com.androidgame.fx.Graphics;
import com.androidgame.fx.ScreenConfig;

public class Player extends Entity {
	
	public Entity	target;
	public int		scrWidth;
	public int		scrHeight;
	
	public Player() {
		image = null;
		position = new Point();
		maxHpRect = new Rect();
		hpRect = new Rect();
		collisionArea = new Rect();
		animations = new Animation[ANI_END];
		currentAnimation = null;
	}
	
	public void init(Bitmap image) {
		this.image = image;
		frameWidth = image.getWidth() / 28;
		frameHeight = image.getHeight() / 8;	
		scrWidth = ScreenConfig.virtualScreenWidth;
		scrHeight = ScreenConfig.virtualScreenHeight;
		
		position.set(1000, 500);
		hp = maxHp = 100;
		power = 30;
		armor = 3;
		speed = 5.0f;
		alive = true;
		state = STATE_IDLE;
		target = null;

		animations[ANI_IDLE] = new Animation(true, 0, 4, 200);
		animations[ANI_WALK] = new Animation(true, 4, 8, 100);
		animations[ANI_ATTACK] = new Animation(false, 12, 4, 100);
		animations[ANI_DEAD] = new Animation(false, 22, 6, 300);
		currentAnimation = animations[ANI_IDLE];
		
		collisionArea = new Rect(position.x - frameWidth / 2, position.y - frameHeight / 2,
								position.x + frameWidth / 2, position.y + frameHeight / 2);
	}
	
	public void update() {
		// dead
		if(compareState(STATE_DEAD)) {
			if(currentAnimation.play == false) {
				alive = false;
			}
		} else { // alive
			processDrt();
			// attacked
			if(compareState(STATE_ATTACK)) {
				// end
				if(currentAnimation.play == false) {
					removeState(STATE_ATTACK);
					removeState(STATE_HIT);
					setAnimation(ANI_IDLE);
				// ing
				} else if(target != null && compareState(STATE_HIT) == false) {
					int dest = getDistance(position.x, position.y, target.position.x, target.position.y);
					if(dest < 80) {
						target.damaged(power);
						addState(STATE_HIT);
					}
				}
			} else if(compareState(STATE_WALK)) {
				movement();
			}
		}
		
		animation();
	}
	
	public void present(Graphics g) {
		int scrX, scrY;
		
		if(position.x < scrWidth / 2) {
			scrX = position.x % scrWidth;
		} else if(position.x > Map.width - scrWidth / 2) {
			scrX = position.x % scrWidth;
		} else {
			scrX = scrWidth / 2;
		}
		
		if(position.y < scrHeight / 2) {
			scrY = position.y % scrHeight;
		} else if(position.y > Map.height - scrHeight / 2) {
			scrY = position.y % scrHeight;
		} else {
			scrY = scrHeight / 2;
		}
		
		maxHpRect.set(scrX - 32, scrY - frameHeight / 3, scrX + 32, scrY - frameHeight / 3 + 10);
		hpRect.set(scrX - 32, scrY - frameHeight / 3, scrX - 32 + 64 * hp / maxHp, scrY - frameHeight / 3 + 10);
		
		g.drawRect(maxHpRect, Color.WHITE);
		g.drawRect(hpRect, Color.RED);
		
		g.drawBitmap(image, scrX - frameWidth / 2, scrY - frameHeight / 2, (currentAnimation.startFrame + frame) * frameWidth, drt * frameHeight, frameWidth, frameHeight);
	}
	
	private void movement() {
		remainX += (float)Math.cos(angle) * speed;
		remainY += (float)-Math.sin(angle) * speed;
		
		move((int)remainX, (int)remainY);
				
		remainX -= (int)remainX;
		remainY -= (int)remainY;
		
		int x = 0;
		int y = 0;
		boolean mapOut = false;
		if(collisionArea.left < 0) {
			x = collisionArea.width() / 2;
			mapOut = true;
		} else if(collisionArea.right > Map.width) {
			x = Map.width - collisionArea.width() / 2;
			mapOut = true;
		} else {
			x = position.x;
		}
		if(collisionArea.top < 0) {
			y = collisionArea.height() / 2;
			mapOut = true;
		} else if(collisionArea.bottom > Map.height){
			y = Map.height - collisionArea.height() / 2;
			mapOut = true;
		} else {
			y = position.y;
		}
		
		if(mapOut == true) setPosition(x, y);
	}
}
